﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShieldSimulatorWebModel;
using ShieldSimulatorWebBLL;
using System.IO;
using System.Web.Script.Serialization;
using ShieldSimulatorWeb.ViewModel;

namespace ShieldSimulatorWeb.Controllers
{
    public class ShieldSimViewController : Controller
    {
        // GET: ShieldSimView
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取最新30条数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetList30Data()
        {
            var dataList = DataManager.GetInstance().GetLastestData(30);
            var viewDataList = new List<ListViewModel>();
            foreach (var data in dataList)
            {
                var viewData = new ListViewModel()
                {
                    Id = data.Id.ToString(),
                    Circle = data.Circle.ToString(),
                    GroundPressure = data.GroundPressure.ToString("f2"),
                    Temperature = data.Temperature.ToString(),
                    Flow = string.Format("{0:F2}", data.Flow),
                    GenerateTime = data.GenerateTime.ToString()
                };
                viewDataList.Add(viewData);
            }
            return Json(viewDataList);
        }
        /// <summary>
        /// 开启模拟器
        /// </summary>
        /// <returns>Json对象</returns>
        [HttpPost]
        public string StartSim()
        {
            ///此处写 到bll中执行开启采集器的方法，并开始收集30条最新数据的收集器功能
            try
            {
                DataCollector.CreateCollector().StartServer();
                return "okay";
            }
            catch (Exception)
            {
                return "nope";
            }
        }

        /// <summary>
        /// 停止模拟
        /// </summary>
        /// <returns>状态字符</returns>
        [HttpPost]
        public string StopSim()
        {
            ///此处写 到bll中执行关闭模拟器的方法
            try
            {
                DataCollector.CreateCollector().StopServer();
                return "okay";
            }
            catch (Exception)
            {
                return "nope";
            }
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <returns>Json对象</returns>
        [HttpPost]
        public JsonResult SearchList()
        {
            var _start = Convert.ToDateTime(Request["startTime"]);
            var _end = Convert.ToDateTime(Request["endTime"]).AddSeconds(1);//+1s的原因是后面貌似查找会丢失最后一个时间点的数据
            ///此处写 到bll中执行搜索数据的方法，根据前台的两个起止时间到后台找到数据后返回Json给前台
            var dataList = DataManager.GetInstance().GetSectionData(_start, _end);
            var viewDataList = new List<ListViewModel>();
            foreach (var data in dataList)
            {
                var viewData = new ListViewModel()
                {
                    Id = data.Id.ToString(),
                    Circle = data.Circle.ToString(),
                    GroundPressure = data.GroundPressure.ToString("f2"),
                    Temperature = data.Temperature.ToString(),
                    Flow = string.Format("{0:F2}", data.Flow),
                    GenerateTime = data.GenerateTime.ToString()
                };
                viewDataList.Add(viewData);
            }
            return Json(viewDataList);
        }

        /// <summary>
        /// 折线绑定
        /// </summary>
        /// <returns>Json对象</returns>
        [HttpPost]
        public JsonResult SearchLine()
        {
            ///此处写 到bll中执行搜索数据的方法，根据前台的两个起止时间到后台找到数据后返回Json给前台
            var _start = Convert.ToDateTime(Request["starttime"]);
            var _end = Convert.ToDateTime(Request["endtime"]).AddSeconds(1);//+1s的原因是后面貌似查找会丢失最后一个时间点的数据
            ///此处写 到bll中执行搜索数据的方法，根据前台的两个起止时间到后台找到数据后返回Json给前台
            var dataList = DataManager.GetInstance().GetSectionData(_start, _end);
            var viewDataList = new List<PolyLineViewModel>();
            foreach (var data in dataList)
            {
                var viewData = new PolyLineViewModel()
                {
                    Circle = data.Circle.ToString(),
                    GroundPressure = data.GroundPressure.ToString("f2"),
                    Temperature = data.Temperature.ToString(),
                    Flow = string.Format("{0:F2}", data.Flow),
                    GenerateTime = data.GenerateTime.ToString()
                };
                viewDataList.Add(viewData);
            }
            return Json(viewDataList);
        }

        /// <summary>
        /// 柱状图绑定
        /// </summary>
        /// <returns>Json对象</returns>
        [HttpPost]
        public JsonResult SearchHistory()
        {
            var _start = Convert.ToDateTime(Request["starttime"]);
            var _end = Convert.ToDateTime(Request["endtime"]).AddSeconds(1);//+1s的原因是后面貌似查找会丢失最后一个时间点的数据
            ///此处写 到bll中执行搜索数据的方法，根据前台的两个起止时间到后台找到数据后返回Json给前台
            var dataList = DataManager.GetInstance().GetHistoryData(_start, _end);
            var viewDataList = new List<BarGraphViewModel>();
            foreach (var data in dataList)
            {
                var viewData = new BarGraphViewModel()
                {
                    MinGroundPressure = data.MinGroundPressure.ToString("f2"),
                    MaxGroundPressure = data.MaxGroundPressure.ToString("f2"),
                    AvgGroundPressure = data.AvgGroundPressure.ToString("f2"),
                    MinTemperature = data.MinTemperature.ToString(),
                    MaxTemperature = data.MaxTemperature.ToString(),
                    AvgTemperature = data.AvgTemperature.ToString(),
                    MinFlow = string.Format("{0:F2}", data.MinFlow),
                    MaxFlow = string.Format("{0:F2}", data.MaxFlow),
                    AvgFlow = string.Format("{0:F2}", data.AvgFlow),
                    TimePoint = data.StartTime.ToString(),
                };
                viewDataList.Add(viewData);
            }
            return Json(viewDataList);
        }

        /// <summary>
        /// 根据id删除单条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteDataById(int id)
        {
            var result = DataManager.GetInstance().DeleteDataById(id);
            return Json(result);
        }

        /// <summary>
        /// 根据model更新单条数据
        /// </summary>
        /// <param name="data">model data</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDataByModel(SimDataModel data)
        {
            var result = DataManager.GetInstance().UpdateDataByModel(data);
            return Json(result);
        }

        /// <summary>
        /// 根据model列表更新多条数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdataDataByModelList(List<SimDataModel> modellist)
        {
            var result = DataManager.GetInstance().UpdataDataByModelList(modellist);
            return Json(result);
        }

        /// <summary>
        /// 根据id数组批量删除数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteDataByIdArray(int[] arrayId)
        {
            var a = arrayId;
            var result = DataManager.GetInstance().DeleteDataByIdArray(arrayId);
            return Json(result);
        }
    }
}